<?php

namespace App\Console\Commands;


use App\Dep\AccountDep;
use App\Dep\JingdongGenreTagCategoryDep;
use App\Dep\JingdongGenreTagDep;
use App\Dep\JingdongTagCategoryDep;
use App\Dep\JingdongTagDep;
use App\Lib\JdWebSdk;
use Illuminate\Console\Command;
use App\Lib\Jos\JosClient;

class SyncCmd extends Command
{
    public $signature = 'content:sync';

    public function handle()
    {
        $this->syncJingdongTagGroup();
        $this->syncJingdongTag();
        $this->syncJingdongGenreTag();
    }

    public function syncJingdongTagGroup()
    {

        $accountDep = new AccountDep();

        $accountDep = new AccountDep();
        $resCookie = $accountDep->firstJingdong();

        if (!$resCookie) {
            $this->info('京东cookie无效');
            log_daily('sync')->info('京东cookie无效');
            return false;
        }

        try {
            $client = new JdWebSdk($resCookie->cookie);
            $resGroup = $client->getTagGroup();
            if (!isset($resGroup['code']) || $resGroup['code'] != 0) {
                $this->info("获取京东标签分组失败：{$resGroup['errorMsg']}");
                log_daily('sync')->info("获取京东标签分组失败：{$resGroup['errorMsg']}");
            }

            $tagCategoryDep = new JingdongTagCategoryDep();
            $genreTagCategoryDep = new JingdongGenreTagCategoryDep();
            foreach ($resGroup['result'] as $key => $value) {
                //兴趣标签
                if ($value['id'] == 632) {
                    $resGroup = $value['children'];
                    $groupData = [];
                    foreach ($resGroup as $item) {
                        $groupData[] = [
                            'platform_id' => $item['id'],
                            'parent_id' => 632,
                            'name' => $item['name'],
                        ];
                    }
                    $tagCategoryDep->truncate();
                    $tagCategoryDep->batchAdd($groupData);
                }

                //体裁标签
                if ($value['id'] == 41) {
                    $resGroup = $value['children'];
                    $groupData = [];
                    foreach ($resGroup as $item) {
                        $groupData[] = [
                            'platform_id' => $item['id'],
                            'parent_id' => 41,
                            'name' => $item['name'],
                        ];
                    }
                    $genreTagCategoryDep->truncate();
                    $genreTagCategoryDep->batchAdd($groupData);
                }
            }

            $this->info('同步京东标签分组完成');
            log_daily('sync')->info("同步京东标签分组完成");

        } catch (\Exception $e) {
            $this->info("同步京东标签分组失败：{$e->getMessage()}");
            log_daily('sync')->info("同步京东标签分组失败：{$e->getMessage()}");
        }

    }

    public function syncJingdongTag()
    {

        $tagCategoryDep = new JingdongTagCategoryDep();
        $tagDep = new JingdongTagDep();

        $accountDep = new AccountDep();
        $resCookie = $accountDep->firstJingdong();

        if (!$resCookie) {
            $this->info('京东cookie无效');
            log_daily('sync')->info('京东cookie无效');
            return false;
        }

        $client = new JdWebSdk($resCookie->cookie);

        $resCategory = $tagCategoryDep->all();

        $data = [];
        $resCategory->each(function ($item) use ($client, &$data) {
            $resTag = $client->getTag($item['platform_id']);
            if (!isset($resTag['code']) || $resTag['code'] != 0) {
                $this->info("获取京东标签失败：{$resTag['errorMsg']}");
                log_daily('sync')->info("获取京东标签失败：{$resTag['errorMsg']}");
                return true;
            }

            $tagArr = $resTag['result'];
            foreach ($tagArr as $tag) {
                $data[] = [
                    'platform_id' => $tag['id'],
                    'category_id' => $item['platform_id'],
                    'secondClassifyId' => $tag['secondClassifyId'],
                    'name' => $tag['name'],
                ];
            }
        });
        $tagDep->truncate();
        $tagDep->batchAdd($data);

        $this->info("同步京东标签完成");
        log_daily('sync')->info("同步京东标签完成");
    }

    public function syncJingdongGenreTag()
    {

        $tagCategoryDep = new JingdongGenreTagCategoryDep();
        $tagDep = new JingdongGenreTagDep();

        $accountDep = new AccountDep();
        $resCookie = $accountDep->firstJingdong();

        if (!$resCookie) {
            $this->info('京东cookie无效');
            log_daily('sync')->info('京东cookie无效');
            return false;
        }

        $client = new JdWebSdk($resCookie->cookie);

        $resCategory = $tagCategoryDep->all();

        $data = [];
        $resCategory->each(function ($item) use ($client, &$data) {
            $resTag = $client->getTag($item['platform_id']);
            if (!isset($resTag['code']) || $resTag['code'] != 0) {
                $this->info("获取京东体裁标签失败：{$resTag['errorMsg']}");
                log_daily('sync')->info("获取京东体裁标签失败：{$resTag['errorMsg']}");
                return true;
            }

            $tagArr = $resTag['result'];
            foreach ($tagArr as $tag) {
                $data[] = [
                    'platform_id' => $tag['id'],
                    'firstClassifyId' => $tag['firstClassifyId'],
                    'secondClassifyId' => $tag['secondClassifyId'],
                    'thirdClassifyId' => $tag['thirdClassifyId'],
                    'name' => $tag['name'],
                ];
            }
        });
        $tagDep->truncate();
        $tagDep->batchAdd($data);

        $this->info("同步京东体裁标签完成");
        log_daily('sync')->info("同步京东体裁标签完成");
    }

}
